import time
import unittest

from configparser import ConfigParser

from Pages.PlanePage import PlanePage
from Pages.AdminPage import AdminPage
from Pages.OrderListPage import OrderListPage
from Pages.tripPage import tripPage
from Pages.OBTFlightDetailPage import OBTFlightDetailPage
from Pages.adminFlightDetailPage import adminFlightDetailPage
from Public import Gl
from Public.Login import Login


class planeTicket(unittest.TestCase):
    # 定义类属性存储订单号（全局共享）
    global_order_no = None
    global_refund_OrderNo = None
    def setUp(self):
        parse = ConfigParser()
        #win需使用配置文件绝对路径
        parse.read('D:\zidonghua\dtsl-master5\dtsl-master3\dtsl-master\DTSL\Public\configurations.ini')
        self.driver = Gl.BrowserEngine(self).open_browser(self)
        self.L = Login()
        self.urlOBT = parse.get('OBT','url')
        self.useridOBT = parse.get('OBT','userid')
        self.passwordOBT = parse.get('OBT','password')
        self.urlAdmin = parse.get('admin','url')
        self.useridAdmin = parse.get('admin','userid')
        self.passwordAdmin = parse.get('admin','password')
        self.flightOrderNo = ''
        self.changeOrderNo = ''
        self.refundOrderNo = ''

    def tearDown(self):
        self.driver.quit()


    def test_01planeTicket(self):
        PP = PlanePage()
        self.L.LoginOBT(self.urlOBT,  self.useridOBT, self.passwordOBT)
        PP.closeNotice()
        #    PP.click_Close()

        PP.select_PlaneFromCity()
        PP.select_PlaneToCity()
        PP.select_PlaneGoDate()
        time.sleep(3)
        PP.click_searchBtn(0)
        time.sleep(2)
        PP.closeNotice()
        #    PP.click_Close()
        PP.reBuyPlaneTiket(1)  #控制买第n行的票,index从1开始
        PP.ignoreRecommend()    #忽略推荐航班继续预订

        # 提交操作
        PP.click_submitPlane()
        time.sleep(5)
        TP = tripPage()
        TP.submitTrip()
        time.sleep(10)
        

        TP.goDFDetail()
        time.sleep(5)
        flightDetailPage = OBTFlightDetailPage()
        planeTicket.flightOrderNo = flightDetailPage.getOrderNo()
        # print(planeTicket.flightOrderNo)
        print(f"首次购票后的订单号：{planeTicket.flightOrderNo}")
        time.sleep(10)

        #继续改签
        flightDetailPage.startChange()
        time.sleep(10)
        PP.reBuyPlaneTiket(1)  #控制买第n趟的航班,index从1开始
        time.sleep(10)
        flightDetailPage.submitChange()
        time.sleep(5)
        # planeTicket.changeOrderNo = flightDetailPage.getOrderNo()
        change_Order_No = flightDetailPage.getOrderNo()
        planeTicket.global_order_no = change_Order_No
        time.sleep(5)
        print(f"改签后的订单号：{change_Order_No}")


    """
    def test_01chooseOrderToChange(self):
        #        global flightOrderNo
        #        flightOrderNo = 'DF240624525758333'
        PP = PlanePage()

        self.L.LoginOBT(self.urlOBT,  self.useridOBT, self.passwordOBT)
        PP.closeNotice()
        PP.goOrderList()

        OL = OrderListPage()
        OL.selectOrder(planeTicket.flightOrderNo)
        flightDetailPage = OBTFlightDetailPage()
        flightDetailPage.startChange()

        PP.reBuyPlaneTiket(1)  #控制买第n趟的航班,index从1开始
        flightDetailPage.submitChange()
        planeTicket.changeOrderNo = flightDetailPage.getOrderNo()
        print(planeTicket.changeOrderNo)
        time.sleep(5)
    """

    '''登录管家后台处理改签单'''
    def test_02flightChange(self):
        AP = AdminPage()
        self.L.LoginAdmin(self.urlAdmin, self.useridAdmin, self.passwordAdmin)
        AP.goToFlightOrder()
        print(f'管家后台处理的改签订单号：{planeTicket.global_order_no}')
        # AP.selectOrder(planeTicket.changeOrderNo)
        AP.selectOrder(planeTicket.global_order_no)
        # AP.selectOrder("DFC250803778973081")
        flightDetailPage = adminFlightDetailPage()
        flightDetailPage.confirmChange()
        flightDetailPage.operateChange()

    '''前台申请退票'''
    def test_03chooseOrderToRefund(self):
        PP = PlanePage()
        self.L.LoginOBT(self.urlOBT,  self.useridOBT, self.passwordOBT)
        PP.goOrderList()
        OL = OrderListPage()
        OL.selectOrder(planeTicket.global_order_no)
        # OL.selectOrder("DFC250803778973081")
        # OL.selectOrder(self.changeOrderNo)
        time.sleep(2)
        flightDetailPage = OBTFlightDetailPage()
        flightDetailPage.applyTicketRefund()
        refund_Order_No = flightDetailPage.getOrderNo()
        planeTicket.global_refund_OrderNo = refund_Order_No
        print(f'前台申请退票后的订单号：{refund_Order_No}')
        time.sleep(2)

    '''登录管家后台处理退票单'''
    def test_04flightRefund(self):
        AP = AdminPage()
        self.L.LoginAdmin(self.urlAdmin, self.useridAdmin, self.passwordAdmin)
        AP.goToFlightOrder()
        AP.selectOrder(planeTicket.global_refund_OrderNo)
        # AP.selectOrder("DFC250727774091107")
        flightDetailPage = adminFlightDetailPage()
        # to do---退票待确认按钮没有找到
        flightDetailPage.confirmRefund()
        print("国内机票退票成功")


if __name__ == '__main__':
    unittest.main()



